<template>
	<my-main title="订单详情" isBg  :isDep="false" isBack @infoFun="infoFun">
		<template #bgHead>
			<view class="flex-between-center">
				<view class="f-40 fwb">
					{{ formData.Header?.OrderStatus || '--' }}
				</view>
				<text class="flex-start-center">
					<text class="m-l-8" :key="index" v-for="(item,index) in formData.Header?.IsUrgentOrder?.filter((el:any)=>el) || []">
						<up-tag :text="item" type="warning" size="mini"></up-tag>
					</text>
				</text>
			</view>
			<view class="alpha-8 f-24 m-t-4 flex-between-center">
				<text>{{ `级别：${formData.Header?.Level || '普通'}` }}</text>
				<text v-if="!['结单', '完工'].includes(formData.Header?.OrderStatus)">
					{{ `距离交期${getDaysUntilDeadline(formData.Header?.EstDeliveryDate || new Date()).dayDiffText}` }}
				</text>
			</view>
		</template>
		<view class="orderDetails">
			<my-cell title="基本信息" isBox>
				<my-text label="订单单号" :value="formData.Header?.OrderCode" :labelWidth="labelWidth" />
				<my-text label="创建日期" :value="formData.Header?.OrderCrtTime" :labelWidth="labelWidth" />
				<my-text label="交货日期" :value="formData.Header?.EstDeliveryDate" :labelWidth="labelWidth" />
				<my-text label="订单状态" :value="formData.Header?.OrderStatus" :labelWidth="labelWidth" />
				<my-text label="付款状态" :value="formData.Header?.PayStatus" :labelWidth="labelWidth" />
				<my-text label="开票状态" :value="formData.Header?.InvoiceStatus" :labelWidth="labelWidth" />
			</my-cell>
			<my-cell title="订单备注" isBox>
				<view class="f-24">
					<up-parse :content="formData.Header?.OrderDesc" v-if="formData.Header?.OrderDesc"></up-parse>
					<text v-else class="text-color-text">无备注</text>
				</view>
			</my-cell>
			<my-cell title="客户信息" isBox>
				<my-text label="客户名称" :value="formData.Header?.ClientName" :labelWidth="labelWidth" />
				<my-text label="联系人" :value="formData.Header?.SubAccountName" :labelWidth="labelWidth" />
				<my-text label="联系电话" :value="formData.Header?.SubAccountPhone" :labelWidth="labelWidth" />
				<my-text label="客户名称" :value="formData.Header?.QQCode" :labelWidth="labelWidth" />
			</my-cell>
			<my-cell title="商品信息" isBox>
				<my-list :options="formData.Goods">
					<template #item="{ row }">
						<my-good :row="row"></my-good>
					</template>
				</my-list>
			</my-cell>
			<my-cell title="快递信息" isBox>
				<my-list :options="formData.Address">
					<template #item="{ row }">
						<view class="f-28 bg-color-bg p-16 radius-8">
							<view class="">{{ `快递方式：${row.ExpressWay || '--'}  ${row.ExpressCode || ''}` }}</view>
							<view class="m-t-8">{{ `收货人：${row.Receiver || '--'}  ${row.CellPhone || ''}` }}</view>
							<view class="m-t-8">{{ `寄送至：${row.Province || '省'}/${row.City || '市'}/${row.Town || '区/县'}/${row.Detail || ''}` }}</view>
						</view>
					</template>
				</my-list>
			</my-cell>
			<my-cell title="自提信息" isBox>
				<my-list :options="formData.SelfAddress">
					<template #item="{ row,index }">
						<view class="f-28 bg-color-bg p-16 radius-8">
							<view class="">{{ `地址${formData.SelfAddress.length>1?index+1:''}：${row.SelfGetAddDetail || '--'}` }}</view>
						</view>
					</template>
				</my-list>
			</my-cell>
			<my-cell title="核销记录" isBox>
				<my-list :options="formData.Payments">
					<template #item="{ row }">
						<view class="f-28 bg-color-bg p-16 radius-8">
							<view>{{ `核销单号：${row.HeXiaoCode || '--'}` }}</view>
							<view>{{ `核销类型：${row.RealPayType || '--'}` }}</view>
							<view>{{ `核销方式：${row.RealPayMethod || '--'}` }}</view>
							<view>{{ `核销金额：${row.HeXiaoAmt || '--'}` }}</view>
							<view>{{ `核销时间：${row.HeXiaoTime || '--'}` }}</view>
							<view>{{ `核销人：${row.OperName || '--'}` }}</view>
						</view>
					</template>
				</my-list>
			</my-cell>
			<my-cell title="其他费用" isBox>
				<up-table>
					<up-tr>
						<up-th>费用名称</up-th>
						<up-th>单价</up-th>
						<up-th>数量</up-th>
						<up-th>总价</up-th>
					</up-tr>
					<up-tr :key="index" v-for="(item, index) in formData.OtherFees || []">
						<up-td>{{ item.ItemName }}</up-td>
						<up-td>{{ item.UnitPrice }}</up-td>
						<up-td>{{ item.Count }}</up-td>
						<up-td>{{ item.TotalPrice }}</up-td>
					</up-tr>
				</up-table>
			</my-cell>
			<my-cell title="其他信息" isBox>
				<my-text label="折扣原因" :value="isStringOfNumber(formData.Header?.DisReason)? `${formData.Header?.DisReason*10}折`:formData.Header?.DisReason" :labelWidth="labelWidth" />
				<my-text label="折扣金额" :value="formData.Header?.EarseAmt || 0" :labelWidth="labelWidth" />
				<my-text label="抹零状态" :value="formData.Header?.RoundOffChange?`已抹零`:'不抹零'" :labelWidth="labelWidth" />
				<my-text label="优惠劵" :value="formData.Header?.DiscountCode" :labelWidth="labelWidth" />
			</my-cell>
			<my-cell title="费用明细" isBox>
				<my-text label="商品金额" :value="`+￥${formData.financial?.GoodAmt || 0}`" :labelWidth="labelWidth" />
				<my-text label="快递费用" :value="`-￥${formData.financial?.ExpressFee || 0}`" :labelWidth="labelWidth" />
				<my-text label="其他费用" :value="`+￥${formData.financial?.OtherFee || 0}`" :labelWidth="labelWidth" />
				<my-text label="优惠券金额" :value="`-￥${formData.financial?.CouponFee || 0}`" :labelWidth="labelWidth" />
				<my-text label="折扣金额" :value="`-￥${formData.financial?.DiscntAmt || 0}`" :labelWidth="labelWidth" />
				<my-text label="抹零金额" :value="`-￥${formData.financial?.RoundOffChangeAmt || 0}`" :labelWidth="labelWidth" />
				<up-divider class="m-t-8 m-b-2" :hairline="true" textPosition="left"></up-divider>
				<my-text label="订单金额" :value="`￥${formData.financial?.OrderAmt || 0}`" :labelWidth="labelWidth" />
				<up-divider class="m-t-8 m-b-2" :hairline="true" textPosition="left"></up-divider>
				<my-text label="已收金额" :value="`￥${formData.financial?.PayAmt || 0}`" :labelWidth="labelWidth" />
				<my-text label="待收金额" :value="`￥${formData.financial?.UnPayAmt || 0}`" :labelWidth="labelWidth" />
			</my-cell>
			<my-cell title="更新日志" isBox>
				<my-log :options="formData.logs" time="CrtTime" title="UserName" contont="OperDesc"></my-log>
			</my-cell>
		</view>
	</my-main>
</template>

<script lang="uts" setup>
//公共状态
import { useVue  } from '@/utils/useVue.uts'
const { reactive, ref,app,Token,onMounted,watch,http,goToPage,formatDate,getTimeAgo,proxy,getDaysUntilDeadline } = useVue()
interface Props {
	ClientCode?:string,
	OrderCode?:string,
}
const props = withDefaults(defineProps<Props>(), {

})
const labelWidth=ref(160)
// 表单数据
const formData:any = reactive({
	Address:[],
	Files:[],
	Goods:[],
	Header:{},
	OtherFees:[],
	Payments:[],
	SelfAddress:[],
	financial:{},
	logs:[],
})
const options=reactive({

})

//结合正则判断是否是整数或浮点数
const isStringOfNumber = (str:string) => {
	return /^-?\d+(\.\d+)?$/.test(str);
}

const getDetails = () => {
	http.request({
			url: 'api/OrderMan/GetOrderDetails',
			data:{
				ClientCode:props.ClientCode,
				OrderCode:props.OrderCode,
			},
			success: (res : any) => {
				formData.Header=res.Header
				formData.financial=res.financial
				formData.Address=res.Address
				formData.SelfAddress=res.SelfAddress
				formData.OtherFees=res.OtherFees
				formData.Goods=res.Goods
			},
			fail:()=>{

			}
		})
}
const getLog = () => {
	http.request({
			url: 'api/OrderMan/GetOrderOperHistory',
			data:{
				ClientCode:props.ClientCode,
				OrderCode:props.OrderCode,
			},
			success: (res : any) => {
				formData.logs=res
			},
			fail:()=>{

			}
		})
}
const getPayments = () => {
	http.request({
			url: 'api/PayOnline/GetHeXiaoOrderCode',
			data:{
				ClientCode:props.ClientCode,
				OrderCode:props.OrderCode,
			},
			success: (res : any) => {
				formData.Payments=res
			},
			fail:()=>{

			}
		})
}
//初始运行
const infoFun = () => {
	getDetails()
	getLog()
	getPayments()
}
onMounted(() => {

})
</script>

<style scoped lang="scss">
.class-name {
	font-weight: bold;
}
.good-attribute {
	display: flex;
	flex-wrap: wrap;
	.good-attribute-item {
		width: calc((100% + 8rpx) / 2 - 8rpx);
		margin-bottom: 2rpx;
		&:not(:nth-child(2n)) {
			margin-right: 8rpx;
		}
	}
}
</style>
